<?php
class ResultModel extends Model
{
    private $db;
    
    function __construct()
    {
        $this->db = DataProvider::getInstance();
    }

    public function viewResult($permission)
    {
        $session = Session::getInstance();
        $id = $session->get('id');
        $data = NULL;
        $con = array('id' => $id);
        $userRS = $this->db->get('users', $con);
        $data['user'] = $userRS->getRow(0);
        switch($session->get('permission'))
        {
            case 1:
                {

                }
                break;
            
            case 2:
                {
                    $sql = "select c.id as classid,s.id as subjectid,s.name as subname,c.semester as semester,c.isfinished as isfinished from classes c,subjects s where  c.subjectid = s.id and c.lectureid = $id";
                    $data['classInfo'] = new ResultSet($this->db->query($sql));                    
                }
                break;

            case 3:
                {
                    $sql = $this->buildSql("u.id = $id");
                    $data['result'] = new ResultSet($this->db->query($sql));                    
                }
                break;

            default:
                break;
        }
        return $data;
    }

    public function studentSearch($type,$keyword)
    {
        $session = Session::getInstance();
        $id = $session->get('id');
        $condition = array('id' => $id);
        $userRS = $this->db->get('users', $condition);
        $data['user'] = $userRS->getRow(0);
        switch($type)
        {
            case 1:
                {
                    $con = array('sub.name' => array('LIKE',"%$keyword%"));
                }
                break;

            case 2:
                {
                $con = array('c.id' => array('LIKE',"%$keyword%"));
                }
                break;

            default:
                Utility::error("Unknown error",null);
                    exit();
                break;
        }
        $con['u.id'] = $id;
        $sql = $this->buildSql($con);
        $data['result'] = new ResultSet($this->db->query($sql));
        $data['search']['keyword'] = $keyword;
        $data['search']['type'] = $type;
        return $data;
    }


    public function lectureSearch()
    {
        $searchFlag = FALSE;
        $data = NULL;
        $session = Session::getInstance();
        $id = $session->get('id');
        $condition = array('id' => $id);
        $userRS = $this->db->get('users', $condition);
        $data['user'] = $userRS->getRow(0);
        
        $sql = "select c.id as classid,s.id as subjectid,s.name as subname,c.semester as semester,c.isfinished as isfinished from classes c,subjects s where  c.subjectid = s.id and c.lectureid = $id";
        $data['classInfo'] = new ResultSet($this->db->query($sql));
        if(isset($_POST['subject_name']) && $_POST['subject_name'] != NULL)
        {
            $con['sub.name'] = array('LIKE',"%$_POST[subject_name]%") ;
            $data['search']['subject_name'] = $_POST['subject_name'];
            $searchFlag = TRUE;
        }

        if(isset($_POST['class_id']) && $_POST['class_id'] != NULL)
        {
            $con['c.id'] = array('LIKE', "%$_POST[class_id]%");
            $data['search']['class_id'] = $_POST['class_id'];
            $searchFlag = TRUE;
        }

        if (isset($_POST['student_name']) && $_POST['student_name'] != NULL)
        {
            $con['u.name'] = array('LIKE', "%$_POST[student_name]%");
            $data['search']['student_name'] = $_POST['student_name'];
            $searchFlag = TRUE;
        }

        if (isset($_POST['student_id']) && $_POST['student_id'] != NULL)
        {
            $con['r.studentid'] = array('LIKE', "%$_POST[student_id]%");
            $data['search']['student_id'] = $_POST['student_id'];
            $searchFlag = TRUE;
        }
        if($searchFlag)
        {
            $con['c.lectureid'] = $id;
            $sql = $this->buildSql($con, 'uid asc');
            $data['result'] = new ResultSet($this->db->query($sql));
        }
        return $data;
    }
    
    private function buildSql($condition = NULL,$order = NULL)
    {
        $sql = "select u.id as uid,u.name as uname,c.id as class_id,sub.name as sub_name,r.mark as mark
                            from results r, users u,classes c,subjects sub
                            where r.studentid = u.id and r.classid = c.id and c.subjectid = sub.id ";
        if(!is_null($condition))
        {
            if(is_array($condition))
            {
                foreach($condition as $key => $value)
                {
                    if (is_array($value))
                    {
                        $sql.= " AND " . $key . " " . $value[0] . " " . "'$value[1]'";
                    }
                    else
                    {
                        $sql .= " AND " . $key . "=" . "'$value'";
                    }
                }
            }
            else
            {
                $sql .=  "AND $condition";
            }
        }

        if(!is_null($order))
        {
            if(is_array($order))
            {
                $sql .= " ORDER BY ";
                foreach($order as $key => $value)
                {
                    $sql .= $key . " " . $value . ",";
                }
                $sql = substr($sql,0,strlen($sql) - 1);
            }
            else
            {
                $sql .= " ORDER BY $order";
            }
        }

        return $sql;
    }
    
    public function adminSearch()
    {
        $searchFlag = FALSE;
        $data = array();
        $sql = "select a.*,u.name as lecture_name
                from
                    (select c.id as class_id,sub.name as subject_name,
                            r.studentid as student_id,u.name as student_name,c.lectureid as lecture_id,r.mark as student_result
                    from results r,classes c,users u,subjects sub
                    where r.studentid = u.id and r.classid = c.id and c.subjectid = sub.id)a ,
                    users u
                where a.lecture_id = u.id";

        if(isset($_POST['classId']) && $_POST['classId'] != NULL)
        {
            $sql .= " AND a.class_id LIKE '%$_POST[classId]%'";
            $searchFlag = TRUE;
            $data['search']['classId'] = $_POST['classId'];
        }

        if (isset($_POST['subName']) && $_POST['subName'] != NULL)
        {
            $sql .= " AND a.subject_name LIKE '%$_POST[subName]%'";
            $searchFlag = TRUE;
            $data['search']['subName'] = $_POST['subName'];
        }

        if (isset($_POST['lectureId']) && $_POST['lectureId'] != NULL)
        {
            $sql .= " AND a.lecture_id LIKE '%$_POST[lectureId]%'";
            $searchFlag = TRUE;
            $data['search']['lectureId'] = $_POST['lectureId'];
        }

        if (isset($_POST['lectureName']) && $_POST['lectureName'] != NULL)
        {
            $sql .= " AND u.name LIKE '%$_POST[lectureName]%'";
            $searchFlag = TRUE;
            $data['search']['lectureName'] = $_POST['lectureName'];
        }

        if (isset($_POST['studentId']) && $_POST['studentId'] != NULL)
        {
            $sql .= " AND a.student_id LIKE '%$_POST[studentId]%'";
            $searchFlag = TRUE;
            $data['search']['studentId'] = $_POST['studentId'];
        }

        if (isset($_POST['studentName']) && $_POST['studentName'] != NULL)
        {
            $sql .= " AND a.student_name LIKE '%$_POST[studentName]%'";
            $searchFlag = TRUE;
            $data['search']['studentName'] = $_POST['studentName'];
        }
        
        $db = DataProvider::getInstance();
        if($searchFlag)
            $rs = new ResultSet($db->query($sql));
        else
            $rs = new ResultSet();
        $data['result'] = $rs;
        return $data;
    }
}
 
